﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Domain;
using System.IO;
using DAO;

/// <summary>
/// Summary description for Utils
/// </summary>
public class Utils
{

    public static System.Web.SessionState.HttpSessionState Session
    {
        get
        {
            return (System.Web.SessionState.HttpSessionState)HttpContext.Current.Session;
        }
    }

    public static Operador operadorLogueado {
        get{
            if (Session["_operadorLogueado"] == null)
            {
                CheckCookieLogin();
                if (Session["_operadorLogueado"] == null)    
                    return null;
            }

            return (Operador) Session["_operadorLogueado"];
        }
        set{
            Session["_operadorLogueado"] = value;
        }
    }

    public static byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        return ms.ToArray();
    }

    public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
        return returnImage;
    }

    public static string Theme {
        get {
            if (Session["_Theme"] == null)
                return "cupertino";
            return Session["_Theme"].ToString();
        }
        set {
            Session["_Theme"] = value;
        }
    }

    public static void CheckCookieLogin()
    {
        HttpCookie cookieRead = System.Web.HttpContext.Current.Request.Cookies["operador"];

        // Read the cookie information and display it.
        if (cookieRead != null)
        {
            string[] data = cookieRead.Value.Split('=');
            if (data[0] == typeof(Usuario).ToString())
            {
                Utils.operadorLogueado = UsuarioDAO.Traer(long.Parse(data[1].Trim()));
                if (Utils.operadorLogueado == null)
                    System.Web.HttpContext.Current.Request.Cookies.Remove("operador");
            }
            else
            {
                Utils.operadorLogueado = EmpresaDAO.Traer(long.Parse(data[1].Trim()));
                if (Utils.operadorLogueado == null)
                    System.Web.HttpContext.Current.Request.Cookies.Remove("operador");
            }
        }
    }

    public static string imageHandlerPath(long? id, bool esEmpresa)
    { 

        if(!id.HasValue)
        {
            if (esEmpresa)
                return "~/static/NoPicture.gif";
            else
                return "~/static/sin_imagen.png";
        }
        
        return "~/ImageHandler.ashx?id=" + id.ToString();
    }

    public static string SecureRedirect(string aRedirigir) {
        return aRedirigir + 
                    (aRedirigir.IndexOf("?") > 0 ? "&" : "?") +
                    QueryStringHash.ReturnIntegrityCheckHash(aRedirigir.IndexOf("?") > 0 ? aRedirigir.Substring(aRedirigir.IndexOf("?") + 1) : "");
    }

    public static bool isMobile()
    {
        return System.Web.HttpContext.Current.Request.UserAgent.ToLower().Contains("mobile");
    }
}
